package com.flipdf2;

import java.awt.Color;
import java.awt.Graphics2D;
import java.util.LinkedList;
import java.util.List;

import com.flipdf2.event.DocumentEvent;
import com.flipdf2.event.DocumentListener;

public class Page {

	private PageSetup setup;
	private List<Stamp> stampList;

	private List<DocumentListener> listenerList;

	public Page(PageSetup setup) {
		this.setup = setup;
		this.stampList = new LinkedList<Stamp>();
		this.listenerList = new LinkedList<DocumentListener>();
	}

	public int getWidth() {
		return setup.getWidth().toScreenPixel();
	}

	public int getHeight() {
		return setup.getHeight().toScreenPixel();
	}

	public List<Stamp> getStampList() {
		return this.stampList;
	}

	public void addStamp(Stamp stamp) {
		this.stampList.add(stamp);
		fireValueChanged();
	}

	public void removeStamp(Stamp stamp) {
		this.stampList.add(stamp);
		fireValueChanged();
	}

	public void addListener(DocumentListener listener) {
		this.listenerList.add(listener);
	}

	public void removeListener(DocumentListener listener) {
		this.listenerList.remove(listener);
	}

	private void fireValueChanged() {
		DocumentEvent e = new DocumentEvent(this);
		for (DocumentListener listener : listenerList) {
			listener.valueChanged(e);
		}
	}

	public void draw(Graphics2D g2) {
		drawPageSheet(g2);
		if (this.stampList != null) {
			for (Stamp stamp : this.stampList) {
//				stamp.draw(g2);
			}
		}
	}

	private void drawPageSheet(Graphics2D g2) {
		if (this.setup == null) {
			throw new NullPointerException("the page setup is null!");
		}
		int width = setup.getWidth().toScreenPixel();
		int height = setup.getHeight().toScreenPixel();

		g2.setColor(Color.BLACK);
		g2.drawRect(0, 0, width, height);
		g2.setColor(Color.WHITE);
		g2.fillRect(0, 0, width - 1, height - 1);
	}
}
